/**
 * 创建ADS常用函数库
 * @author Lonovo
 */
(function(){
    if (!window.ADS) {
        window['ADS'] = {}
    }
	/**检测浏览器与库的兼容*/
    function isCompatible(other){
		if(other==false || !Array.prototype.push ||
		   !Object.hasOwnProperty || !document.createElement ||
		   !document.getElementsByTagName){
		   	return false;
		}
		return true;
	}
	window['ADS']['isCompatible'] = isCompatible;
	
	/**ADS.$()*/
    function $(){
        var elements = [];
		for(var i=0; i<arguments.length; i++){
			var element = arguments[i];
			if(typeof element == "string"){
				element = document.getElementById(element);
			}
			//只有一个参数
			if(arguments.length==1){
				return element;
			}
			//多个参数
			elements.push(element);
		}
		return elements;
    }
    window['ADS']['$'] = $;
	
	/**事件侦听*/
	function addEvent(node,type,listener){
		if(!(node = $(node))) return false;  //是束接受Id
		
		if(node.addEventListener){
			node.addEventListener(type,listener,false);
		} else if (node.attachEvent){ 
		    //MSIE的方法
			node['e' + type + listener] = listener;    /**定义作用！！方法名为自定义的约定*/
			node[type + listener] = function(){
				node['e' + type + listener](window.event)
			}
			node.attachEvent('on' + type,node[type + listener]);
			return true;
		}
		return false;
	}
	window['ADS']['addEvent'] = addEvent;
	
	/**移除侦听*/
    function removeEvent(node,type,listener){
		if(!(node=$(node))) return false;
		
		if(node.removeEventListener){
			node.removeEventListener(type,listener,false);
			return true;
		} else if(node.detachEvent){
			node.detachEvent('on' + type,node[type + listener]);
			node[type + listener] = null;
			return true;
		}
		return false;
	}
	window['ADS']['removeEvent'] = removeEvent;
	
	/**显示隐藏某节点*/
	function toggleDisplay(node,value){
		if(!(node = $(node))) return false;
		
		if(node.style.display != 'none'){
			node.style.display = 'none'
		}else{
			node.style.display = value || '';
		}
		return true;
	}
	window['ADS']['toggleDisplay'] = toggleDisplay;
	
	/**
	 * 向某一节后添加节点
	 * @param {Object} node  	   要插入的节点
	 * @param {Object} referNode   接受插入的节点
	 */
	function insertAfter(node,referNode){
		return referNode.parentNode.insertBefore(node,referNode.nextSibling)
	}
	window['ADS']['insertAfter'] = insertAfter;
	
	/**删除节点的所有子节点*/
	function removeChildren(parent){
		//当存在子节点时删除该子节点
		while(parent.firstNode){
			parent.firstNode.parentNode.removeChild(parent.firstNode);
		}
		return parent;
	}
	window['ADS']['removeChildren'] = removeChildren;
	
})()
